<template>
  <div class="employees">
    <!-- 工具栏组件 -->
    <mytools>
      <template #right>
        <el-button size="mini" type="primary" @click="toimport">导入</el-button>
        <el-button size="mini" type="primary" @click="opedAdd"
          ><i class="el-icon-plus"></i>新增员工</el-button
        >
      </template>
    </mytools>
    <!-- 表格数据 -->
    <el-card class="mycard">
      <el-table :data="list">
        <el-table-column
          label="序号"
          type="index"
          fixed="left"
        ></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="头像">
          <template slot-scope="{ row }">
            <img
              class="avatar"
              :src="row.staffPhoto"
              v-errimg="defaultImg"
              @click="opendimg(row.staffPhoto)"
            />
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile"></el-table-column>
        <el-table-column label="工号" prop="workNumber"></el-table-column>
        <el-table-column label="聘用形势" prop="formOfEmployment">
          <template slot-scope="{ row }">
            {{ row.formOfEmployment | myEmpolyees }}
          </template></el-table-column
        >
        <el-table-column label="部门" prop="departmentName"></el-table-column>
        <el-table-column label="入职时间" prop="timeOfEntry">
          <template slot-scope="{ row }">{{
            row.timeOfEntry | timeFormata
          }}</template></el-table-column
        >
        <el-table-column label="是否在职">在职</el-table-column>
        <el-table-column label="状态" prop="name">在职</el-table-column>
        <el-table-column label="操作" width="260" fixed="right">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              @click="toUserInfo(row)"
              :disabled="'employees-check' | hasRight"
              >查看</el-button
            >
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text" @click="openRole(row)">角色</el-button>
            <el-button type="text">删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <processpanel
      @updateData="getEmployeesAllList"
      :dialogShow.sync="dialogShow"
    ></processpanel>
    <el-dialog title="查看图片的二维码" :visible.sync="ewmShow">
      <!-- 显示二维码 -->
      <canvas ref="myCanvas" />
    </el-dialog>
    <!-- 设置角色 -->
    <el-dialog title="分配角色" :visible.sync="roleShow">
      <el-form>
        <el-form-item>
          {{ checklist }}
          <!-- 复选框组 -->
          <el-checkbox-group v-model="checklist">
            <el-checkbox
              v-for="(item, index) in roleList"
              :key="index"
              :label="item.id"
            >
              {{ item.name }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item class="myitem">
          <el-button @click="roleShow = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import QrCode from 'qrcode'
import {
  getEmployeesAllListApi,
  getEmployeesApi,
  setEmployeesRoleApi
} from '@/api/Employees.js'
import { getUsersRolesApi } from '@/api/setting'
export default {
  data () {
    return {
      // 员工列表数据源
      list: [{ name: 'xjj' }],
      // 当前页
      currentPage: 1,
      // 页容量的集合
      pageSizes: [5, 6, 7, 8],
      // 当前页容量
      pageSize: 5,
      // 总条数
      total: 0,
      dialogShow: false,
      dialogVisible: false,
      defaultImg: require('@/assets/common/avatar.jpg'),
      ewmShow: false,
      roleShow: false,
      checklist: [],
      roleList: [],
      currentObj: {}
    }
  },
  methods: {
    opedAdd () {
      this.dialogShow = true
    },
    async submit () {
      await setEmployeesRoleApi({
        id: this.currentObj.id,
        roleIds: this.checklist
      })
      // 提示修改成功
      this.$message.success('角色分配成功')
      // 关闭面板
      this.roleShow = false
      // 更新数据
      this.getEmployeesAllList()
    },
    async getUsersRoles () {
      try {
        const res = await getUsersRolesApi()
        this.roleList = res.data.rows
      } catch (error) {}
    },
    async openRole (row) {
      this.roleShow = true
      this.currentObj = row
      const res = await getEmployeesApi(row.id)
      this.checklist = res.data.roleIds
    },
    async getEmployeesAllList () {
      const res = await getEmployeesAllListApi({
        page: this.currentPage,
        size: this.pageSize
      })
      this.list = res.data.rows
      this.total = res.data.total
      console.log(res)
    },
    // 页容量改变时触发
    handleSizeChange (size) {
      this.pageSize = size
      this.getEmployeesAllList()
    },
    // 当前页改变时触发
    handleCurrentChange (page) {
      this.currentPage = page
      this.getEmployeesAllList()
    },
    toimport () {
      this.$router.push('/employees/import')
    },
    toUserInfo (row) {
      this.$router.push(`/employees/detail/${row.id}`)
    },
    opendimg (img) {
      this.ewmShow = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.myCanvas, img, {
          width: 200,
          height: 200
        })
      })
    }
  },
  components: {
    processpanel: () => import('./components/processpanel.vue')
  },
  created () {
    this.getEmployeesAllList()
    this.getUsersRoles()
  }
}
</script>

<style lang="scss">
.employees {
  margin: 20px;
  .mycard {
    margin-top: 20px;
  }
  .el-table__header {
    th:last-child {
      .cell {
        text-align: center;
      }
    }
  }
  .el-pager li.active {
    color: #529ecf;
    cursor: default;
  }
  .el-button--text {
    color: #529ecf;
  }
  .avatar {
    width: 100px;
    height: 100px;
  }
}
</style>
